package com.sys.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.sys.service.LogInfoService;
import com.sys.system.Constants;


/**
 * Created by IntelliJ IDEA. User: admin Date: 2008-2-19 Time: 13:48:33 To
 * change this template use File | Settings | File Templates.
 */
public class GftFilter implements Filter {
	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
        String url = ((HttpServletRequest) request).getRequestURL().toString();
        boolean bool = url.endsWith(".html") || url.endsWith("index.jsp");

        if (!bool) {//如果直接访问的路径是个html或者/gft/结尾的直接放过,否则走下面
			Log log = LogFactory.getLog(this.getClass());
			//response.setCharacterEncoding("UTF-8");

			HttpServletRequest httpRequest = (HttpServletRequest) request;
			HttpSession session = httpRequest.getSession();

			log.debug("权限校验");
			if (null == session.getAttribute(Constants.USERBEAN)
					&& !"login".equals(request.getParameter("op"))
					&& !"valCode".equals(request.getParameter("op"))
                    && !("go".equals(request.getParameter("op")) && "login".equals(request.getParameter("go")))) {
				RequestDispatcher dispatcher = session.getServletContext()
						.getRequestDispatcher("/gftuser.x?op=logout");
				session.setAttribute("request", request);
				session.setAttribute("response", response);
				dispatcher.forward(request, response);
				return;
			}

			log.debug("处理日志");
			ServletContext context = httpRequest.getSession()
					.getServletContext();
			LogInfoService service = (LogInfoService) WebApplicationContextUtils
					.getWebApplicationContext(context).getBean(
							"LogInfoServiceImpl");
			service.insertLogInfo(httpRequest, session);
			
			if (service.inspectionRight(httpRequest, session)) {
				chain.doFilter(request, response);
			} else {
				RequestDispatcher dispatcher = session.getServletContext()
						.getRequestDispatcher("/admin/noright.html");
				session.setAttribute("request", request);
				session.setAttribute("response", response);
				dispatcher.forward(request, response);
				return;
			}
		} else {
			//什么都不做,直接将参数传到后面的方法
			chain.doFilter(request, response);
        }
    }

	public void init(FilterConfig arg0) throws ServletException {
	}
}
